package com.mmy.service.plane.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.UtilConstants;
import com.mmy.dao.plane.TPlaneAirportRepository;
import com.mmy.dao.plane.VPlaneAirportRepository;
import com.mmy.domain.plane.TPlaneAirport;
import com.mmy.domain.plane.VPlaneAirport;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ServiceCommon;
import com.mmy.service.plane.PlaneAirportService;

@Service
public class PlaneAirportServiceImpl implements PlaneAirportService {

	@Autowired
	TPlaneAirportRepository tPlaneAirportRepository;
	@Autowired
	VPlaneAirportRepository vPlaneAirportRepository;
	@Autowired
	ServiceCommon common;

	/**
	 * 函数功能说明：后台航空公司信息管理 显示航空公司基本信息
	 *
	 * @param 参数说明 vPlaneOrg
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人hanxl
	 * @date 创建日期 2020.11.18 修改时间2021.03.16
	 */
	public List<VPlaneAirport> findAll() {
		List<VPlaneAirport> lstPlaneAirport = vPlaneAirportRepository.findAll();
		return lstPlaneAirport;
	}

	/**
	 * 后台机场信息管理 显示航空公司基本信息 异步加载表格
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public List<VPlaneAirport> findSearch(String strAirportName, int startIndex, int count) {
		if (strAirportName == null)
			strAirportName = "";
		List<VPlaneAirport> latPlaneAirport = vPlaneAirportRepository.findSearchALL(strAirportName, startIndex, count);

		return latPlaneAirport;
	}

	/**
	 * 后台机场信息管理中显示全部 分页
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public int queryCount(String strAirportName) {
		if (strAirportName == null)
			strAirportName = "";
		int count = vPlaneAirportRepository.countSearchALL(strAirportName);

		return count;
	}

	/**
	 * 后台创建机场信息提交
	 * 
	 * @return 返回值说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void createPlaneAirPort(TPlaneAirport tPlaneAirport, TSysUser sessionSysUser) {

		// TODO Auto-generated method stub
		// 获取最大值
		tPlaneAirport.setAirportID(common.createBaseCode(UtilConstants.TableName.T_PLANE_AIRPORT));
		tPlaneAirport.setCreateDate(new Date());
		tPlaneAirport.setCreator(sessionSysUser.getUserName());
		tPlaneAirport.setSpare1(String.valueOf(sessionSysUser.getOrgID()));
		tPlaneAirportRepository.save(tPlaneAirport.getAirportID());
		tPlaneAirportRepository.save(tPlaneAirport);
	}

	/**
	 * 获取最大值
	 * 
	 * @return 返回值说明 最大ID
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public int getMaxAirportID() {
		// 流水号
		Object objCode = tPlaneAirportRepository.getMaxAirportID();
		int iID = 0;
		if (objCode != null) {
			iID = Integer.parseInt(objCode.toString());
		}
		return iID;
	}

	/**
	 * 通过id查找
	 * 
	 * @param
	 * @return
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public TPlaneAirport findByAirportID(int iAirportID) {

		return tPlaneAirportRepository.findByAirportID(iAirportID);
	}

	/**
	 * 修改机场提交
	 * 
	 * @return 返回值说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void updatePlaneAirport(TPlaneAirport tPlaneAirport, TSysUser sessionSysUser) {
//		 定义一个新的实体类对象 
		TPlaneAirport dbPlaneAirport = tPlaneAirportRepository.findByAirportID(tPlaneAirport.getAirportID());

		dbPlaneAirport.setAirportName(tPlaneAirport.getAirportName());
		dbPlaneAirport.setZoneID(tPlaneAirport.getZoneID());
		dbPlaneAirport.setMemo(tPlaneAirport.getMemo());
		dbPlaneAirport.setModifyDate(new Date());

		dbPlaneAirport.setReviser(sessionSysUser.getUserName());
		tPlaneAirportRepository.save(dbPlaneAirport);
	}

	/**
	 * 函数功能说明： 根据地区查询所在的机场
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人hanxl
	 * @date 创建日期2021.03.17
	 */
	public List<VPlaneAirport> findByZoneID(int iZoneID) {

		List<VPlaneAirport> lstPlaneAirport = new ArrayList<VPlaneAirport>();
		// 创建国外机场时 选择中国为出发地或者到达地显示中国所有机场
		if (iZoneID == 903156) {
			lstPlaneAirport = vPlaneAirportRepository.getByZoneID();
		} else if (iZoneID == 903158) {
			iZoneID = 710000;
			lstPlaneAirport = vPlaneAirportRepository.getByZoneID(iZoneID);
		} else if (iZoneID == 903344) {
			iZoneID = 810000;
			lstPlaneAirport = vPlaneAirportRepository.getByZoneID(iZoneID);
		} else {

			lstPlaneAirport = vPlaneAirportRepository.findByZoneID(iZoneID);
		}

		return lstPlaneAirport;
	}

}
